package com.baidu.bos.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

import javax.annotation.Resource;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 流程定义管理的action
 * @author Administrator
 *
 */
@Controller
@Scope("prototype")
public class ProcessDefinitionAction extends ActionSupport{
	@Resource
	private ProcessEngine processEngine;
	
	private File deploy;
	private String id;
	public void setId(String id) {
		this.id = id;
	}

	public void setDeploy(File deploy) {
		this.deploy = deploy;
	}
	
	/**
	 * 查询最新版本的流程定义对象
	 * @return
	 */
	public String list(){
		ProcessDefinitionQuery query = processEngine.getRepositoryService().createProcessDefinitionQuery();
		List<ProcessDefinition> list = query.latestVersion().list();
		//将查询到的list集合压入值栈中
		ActionContext.getContext().getValueStack().set("list", list);
		return "list";
	}
	
	//部署新流程
	public String deploy() throws FileNotFoundException{
		//获得部署对象,加载zip压缩文件
		DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
		ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(deploy));
		deploymentBuilder.addZipInputStream(zipInputStream);
		//部署对象
		deploymentBuilder.deploy();
		return "toList";
	}
	
	//查看流程图
	public String viewpng(){
		//通过id查询当前流程定义对应png图片输入流
		InputStream pngStream = processEngine.getRepositoryService().getProcessDiagram(id);
		ActionContext.getContext().getValueStack().set("pngStream", pngStream);
		return "viewpng";
	}
}
